package com.itboye.eweilisten.me.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.TimeCount;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.popwindow.UIAlertView;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.EmailAutoCompleteTextView;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import static android.R.attr.type;

public class ForgetPassWord extends BaseActivity implements UIAlertView.ClickListenerInterface, Observer {

    TextView tvTitle, txt_send_yzm;
    ToggleButton pass1_tb, pass2_tb;
    EditText ediTxt_pass, ediTxt_pass_re, ediTxt_yzm;
    private UIAlertView uiAlertView;
    ImageView back;
    Button ok;
    UserPresenter presenter;
    String SID;
    EmailAutoCompleteTextView ediTxt_email;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass_word);
        tvTitle.setText("密码重置");
        presenter = new UserPresenter(this);
        SID = (String) SPUtils.get(MyApplication.instance, null, Const.S_ID, "");
    }

    TimeCount time;

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ok:
                String account = ediTxt_email.getText().toString();
                String yzm = ediTxt_yzm.getText().toString();
                String pass = ediTxt_pass.getText().toString();
                String pass2 = ediTxt_pass_re.getText().toString();
                if (account.equals("") || yzm.equals("") || pass.equals("") || pass2.equals("")) {
                    MAlert.alert("请先填写完整信息");
                    return;
                }
                if (!pass.equals(pass2)) {
                    MAlert.alert("两次密码不一致");
                    return;
                }
                if (pass.length() != 6) {
                    MAlert.alert("请输入6位密码");
                    return;
                }
                showProgressDialog("修改中...", true);
                presenter.onPassWord(SID, yzm, account, pass2,"");
                break;
            case R.id.pass1_tb:
                if (pass1_tb.isChecked()) {
                    ediTxt_pass
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ediTxt_pass.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }

                break;
            case R.id.txt_send_yzm:
                if (ediTxt_email.getText().toString().equals("") || ediTxt_email.getText().toString() == null) {
                    MAlert.alert("请填写邮箱地址");
                    return;
                } else {
                    presenter.onSecurCode(ediTxt_email.getText().toString(), "2");
                    time = new TimeCount(60000, 1000, txt_send_yzm);// 构造CountDownTimer对象
                    time.start();
                }
                break;
            case R.id.pass2_tb:
                if (pass2_tb.isChecked()) {
                    ediTxt_pass_re
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ediTxt_pass_re.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }

                break;
        }
    }

    @Override
    public void doLeft() {
        uiAlertView.dismiss();
    }

    @Override
    public void doRight() {

    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.send_code_success) {
                uiAlertView = new UIAlertView(this, "温馨提示！", "验证码已发送至预留邮箱", "好", "", type);
                uiAlertView.show();
                uiAlertView.setClicklistener(this);
            } else if (entity.getEventType() == presenter.WangJiMiMa_success) {
                MAlert.alert("" + entity.getData());
                finish();
                try {
                    closeProgressDialog();
                } catch (Exception e) {

                }

            }
        }
    }
}
